export const createEquipmentOptions = (lineList, dict) => {
	const typeDict = dict.find((dict) => dict.id === "8f73d56a-7405-4a3d-a327-966e779af499");
	const typeKeyMap =
		typeDict?.children.reduce((acc, cur) => {
			acc[cur.value] = cur.label;
			return acc;
		}, {}) || {};
	const typeIdMap =
		typeDict?.children.reduce((acc, cur) => {
			acc[cur.value] = cur.id;
			return acc;
		}, {}) || {};
	return lineList.map((line) => {
		const lineOption = {
			text: line.name,
			value: line.id,
			children: [],
		};
		if (!line.locations || line.locations.length === 0) {
			return lineOption;
		}
		lineOption.children = line.locations.map((location) => {
			const stationOption = {
				text: location.name,
				value: location.id,
				children: [],
			};
			if (!location.equipmentList || location.equipmentList.length === 0) {
				return stationOption;
			}
			const systemMap = {};
			location.equipmentList.forEach((equipmentItem) => {
				const equipmentId = equipmentItem.id;
				const equipment = equipmentItem.equipment || {};
				const equipmentTypeValue = equipment.type;
				const equipmentTypeName = typeKeyMap[equipmentTypeValue] ?? equipmentTypeValue;
				const systemId = typeIdMap[equipmentTypeValue] || `${location.id}-${equipmentTypeName}`;
				const cxhtLocationForm = equipmentItem.cxhtLocationForm || {};
				const formId = cxhtLocationForm.id;
				const formPerformance = cxhtLocationForm.performance;
				if (!systemMap[equipmentTypeName]) {
					systemMap[equipmentTypeName] = {
						text: equipmentTypeName,
						value: systemId,
						children: [],
					};
				}
				systemMap[equipmentTypeName].children.push({
					text: equipmentItem.name,
					value: equipmentId,
					equipmentInfo: {
						id: equipmentId,
						name: equipment.name || equipmentItem.name,
						manufacturer: equipment.manufacturer || "",
						type: equipmentTypeName,
						itemName: equipmentItem.name,
					},
					formInfo: {
						id: formId,
						performance: formPerformance,
					},
				});
			});
			stationOption.children = Object.values(systemMap);
			return stationOption;
		});
		return lineOption;
	});
};
